package com.dataseek.iot.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dataseek.iot.product.entity.PdtCategory;
import com.dataseek.iot.product.entity.PdtList;
import com.dataseek.iot.product.mapper.PdtCategoryMapper;
import com.dataseek.iot.product.mapper.PdtListMapper;
import com.dataseek.iot.product.service.CtgService;
import com.dataseek.iot.product.vo.CtgVo;
import com.dataseek.iot.toolikit.common.enums.RspStatusEnum;
import com.dataseek.iot.toolikit.common.service.IDServiceHelper;
import com.dataseek.iot.toolkit.tenant.config.DynamicDataSourceContextHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @discription:
 * @author: Freya.Yan
 * @date: 2022/1/11
 */
@Service
@Slf4j
public class CtgServiceImpl implements CtgService {
    @Autowired
    private IDServiceHelper idServiceHelper;

    @Autowired
    private PdtCategoryMapper pdtCategoryMapper;

    @Autowired
    private PdtListMapper pdtListMapper;


    @Override
    public List<CtgVo> queryCtg(String ctgName) {

        CtgVo attrVo = new CtgVo();
        QueryWrapper<PdtCategory> qw = new QueryWrapper<>();
        if (ctgName != null && !ctgName.isEmpty()) {
            qw.like("ctg_name", ctgName);
        }
        qw.orderByAsc("id");
        List<PdtCategory> list = pdtCategoryMapper.selectList(qw);
        List<CtgVo> rootlist=new ArrayList<>();
        //遍历所有数据，找到根节点菜单
        for (PdtCategory pdtCategory:list) {
            if(pdtCategory.getParentId() == 0){
                CtgVo ctgVo = new CtgVo();
                ctgVo.setId(pdtCategory.getId());
                ctgVo.setCtgName(pdtCategory.getCtgName());
                ctgVo.setCtgCode(pdtCategory.getCtgCode());
                ctgVo.setParentId(pdtCategory.getParentId());
                ctgVo.setCreateTime(pdtCategory.getCreateTime());
                //找到根节点菜单的时候，寻找这个根节点菜单下的子节点菜单。
                findChilds(ctgVo,list);
                //添加到根节点的列表中
                rootlist.add(ctgVo);
            }
        }
        return rootlist;
    }

    //新增部门信息
    @Override
    public Integer insertCtg(PdtCategory pdtCategory) {
        String tenantCode = DynamicDataSourceContextHolder.getDataSourceKey();
        int resultCode = 0;
        long id = idServiceHelper.nextId();
        pdtCategory.setId(id);
        String code = pdtCategory.getCtgName();
        QueryWrapper<PdtCategory> qw = new QueryWrapper<>();
        qw.eq("ctg_name", code);
        Integer count = pdtCategoryMapper.selectCount(qw);
        if (count == 0) {
           // pdtAttributeDic.setTenantCode(tenantCode);
            pdtCategoryMapper.insert(pdtCategory);
        } else {
            resultCode = RspStatusEnum.FAIL.getCode();
        }
        return resultCode;

    }

    @Override
    public Integer updateCtg(PdtCategory pdtCategory) {
        int resultCode = pdtCategoryMapper.updateById(pdtCategory);
        return resultCode;
    }

    @Override
    public Integer deleteCtg(List<Long> tenantIds) {
        Integer resultCode = 0;
        Integer count = 0;
        for (int i = 0; i < tenantIds.size(); i++) {
            long id = tenantIds.get(i);
            PdtCategory pdtCategory = pdtCategoryMapper.selectById(id);
            String ctgName = pdtCategory.getCtgName();
            QueryWrapper<PdtList> qw = new QueryWrapper<>();
            qw.eq("ctg_name", ctgName);
            count = count + pdtListMapper.selectCount(qw);
        }
        if(count> 0 ){
            resultCode = RspStatusEnum.FAIL.getCode();
        } else {
            resultCode = pdtCategoryMapper.deleteBatchIds(tenantIds);
        }
        return resultCode;
    }

    public static void findChilds(CtgVo root, List<PdtCategory> list){
        List<CtgVo> childlist=new ArrayList<>();
        //遍历所有数据，找到是入参父节点的子节点的数据，然后加到childlist集合中。
        for (PdtCategory pdtCategory :
                list) {
            Long pId = pdtCategory.getParentId();
            if (pId !=null && root.getId()==pId) {
                CtgVo ctgVo = new CtgVo();
                ctgVo.setId(pdtCategory.getId());
                ctgVo.setCtgName(pdtCategory.getCtgName());
                ctgVo.setCtgCode(pdtCategory.getCtgCode());
                ctgVo.setParentId(pdtCategory.getParentId());
                ctgVo.setCreateTime(pdtCategory.getCreateTime());
                childlist.add(ctgVo);
            }
        }
        //若子节点不存在，那么就不必再遍历子节点中的子节点了 直接返回。
        if(childlist.size()==0) {
            return;
        }
        //设置父节点的子节点列表
        root.setChilds(childlist);
        //若子节点存在，接着递归调用该方法，寻找子节点的子节点。
        for (CtgVo childs :
                childlist) {
            findChilds(childs, list);
        }
    }
}
